package com.weibo.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.weibo.common.MD5;
import com.weibo.dao.IUserDao;
import com.weibo.pojo.Tuser;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

	@Override
	public Tuser getUserById(Integer userId) {
		HibernateTemplate htp = this.getHibernateTemplate();
		Tuser user = htp.get(Tuser.class, userId);
		return user;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExistUser(Tuser user) throws Exception {
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = "from Tuser where userName = ? and userPass = ?";
		// 将用户的密码MD5处理；
		List<Tuser> userList = htp.find(hql, new Object[] { user.getUserName(),
				new MD5().getMD5ofStr(user.getUserPass()) });
		if (userList.size() > 0)
			return true;
		else
			return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean isExistUser(String userName) throws Exception {
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = "from Tuser where userName = ?";
		List<Tuser> userList = htp.find(hql, userName);
		if (userList.size() > 0)
			return true;
		else
			return false;
	}

	@Override
	public void saveUser(Tuser user) throws Exception {
		user.setUserPass(new MD5().getMD5ofStr(user.getUserPass()));
		System.out.println(user.getUserName() + "-->" + user.getUserPass());
		this.getHibernateTemplate().save(user);
	}
	/**
	 * 通过用户的名字得到用户对象
	 */
	@Override
	public Tuser getUserByName(String userName) throws Exception {
		String hql = "from Tuser where userName = ?";
		return (Tuser)this.getHibernateTemplate().find(hql, userName).get(0);
	}
	
	
	

}
